package com.project.demo.service.impi;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.demo.entity.Admin;
import com.project.demo.entity.Product;
import com.project.demo.mapper.ProductMapper;
import com.project.demo.service.ProductService;
import com.project.demo.vo.AllProductVo;
import com.project.demo.vo.ProductVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
@Service
public class ProductServiceimpi implements ProductService {
    @Autowired
    private ProductMapper productmapper;

    @Override
    public ProductVo<AllProductVo> FindProductVo(Integer page, Integer limit) {
        ProductVo productVo = new ProductVo();
        productVo.setCode(0);
        productVo.setMsg("");
        IPage<Product> productIPage = new Page<>(page, limit);
        IPage<Product> result = productmapper.selectPage(productIPage, null);
        productVo.setCount(result.getTotal());
        List<Product> productList = result.getRecords();
        List<AllProductVo> allProductVoList = new ArrayList<>();
        for (Product product1 : productList) {
            AllProductVo allProductVo = new AllProductVo();
            BeanUtils.copyProperties(product1, allProductVo);
            allProductVoList.add(allProductVo);
        }
        productVo.setData(allProductVoList);
        return productVo;
    }

    @Override
    public ProductVo<AllProductVo> FindAllProductVo() {
        ProductVo productVo = new ProductVo();
        productVo.setCode(0);
        productVo.setMsg("");
        productVo.setCount(Long.valueOf(productmapper.selectCount(null)));
        List<Product> productList = productmapper.selectList(null);
        List<AllProductVo> allProductVoList = new ArrayList<>();
        for (Product product1 : productList) {
            AllProductVo allProductVo = new AllProductVo();
            BeanUtils.copyProperties(product1, allProductVo);
            allProductVoList.add(allProductVo);
        }
        productVo.setData(allProductVoList);
        return productVo;
    }

    @Override
    public Integer deletevo(Integer id) {
        return productmapper.deleteVo(id);
    }

    @Override
    public Integer insertvo(Integer id, String zcid, String zcname, Integer money, String zcclass, String zcperson, String begint) {
        return productmapper.insertVo(id, zcid, zcname, money, zcclass, zcperson, begint);
    }


}